package edu.mtsu.csci3033;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;

import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TeamProject2 extends Applet implements ActionListener, Runnable
{
    Button btnSubmit = new Button("Submit");
    Label lblX = new Label("X:");
    Label lblY = new Label("Y:");;
    TextField txtBoxX = new TextField(10);
    TextField txtBoxY = new TextField(10);
    JPanel inputPanel = new JPanel(new FlowLayout());
	
    Grid BugGrid = new Grid();
    Image Swatter;
    Point SwatterPnt = new Point();
    Point GuessPnt = new Point();
    Point OldGuessPnt = new Point();
	public void init()
	{
		inputPanel.add(lblX);
		inputPanel.add(txtBoxX);
		
		inputPanel.add(lblY);
		inputPanel.add(txtBoxY);
		btnSubmit.addActionListener(this);
		inputPanel.add(btnSubmit);
		add(inputPanel, BorderLayout.SOUTH);
		
		Swatter = getImage(getCodeBase(), "images/FlySwatter.gif");
		super.resize(700, 700);
		validate();
	}
 	
	public void paint(Graphics g)
  	{
   		BugGrid.draw(g);
   		g.drawImage(Swatter, SwatterPnt.x, SwatterPnt.y, 25, 25, null);
   		validate();
  	}


	public void actionPerformed(ActionEvent arg0) 
	{
		// TODO Auto-generated method stub
		int x = -1;
		int y = -1;
		try {
            x = Integer.parseInt(txtBoxX.getText());
            
        }catch (NumberFormatException nx) {
            JOptionPane.showMessageDialog(null, "Enter valid X-cordinate");
        }
		try {
            y = Integer.parseInt(txtBoxY.getText());
            
        }catch (NumberFormatException nx) {
            JOptionPane.showMessageDialog(null, "Enter valid Y-cordinate");
        }
        OldGuessPnt = GuessPnt;
        GuessPnt.x = x;
        GuessPnt.y = y;
        
        SwatterPnt.x = BugGrid.getX(x-1);	//Fix method in Grid class
        SwatterPnt.y = BugGrid.getY(y-1);
        
        
        System.out.println("old GuessPnt = " + OldGuessPnt.toString());
        System.out.println("New GuessPnt = " + GuessPnt.toString());
		repaint();
		start();
	}

	public void start()
	{
		//Start a new thread
		Thread t = new Thread(this);
		t.start();
	}
	
	public void run() 
	{
		// TODO Auto-generated method stub
		
	}
	
	private int getSlope(Point Pnt1, Point Pnt2)
	{
		Point Slope = new Point();
		Slope.y = Pnt2.y - Pnt1.y;
		Slope.x = Pnt2.x - Pnt1.x;
		return Slope.y/Slope.x;
	}
}

